package cn.migu.music.hangupsms.repository;

import java.time.LocalDateTime;
import java.util.List;

import org.apache.ibatis.annotations.*;

import cn.migu.music.hangupsms.common.entity.BlackUserInfo;

/**
 * 黑名单查询
 **/
@Mapper
public interface BlackUserInfoDbMapper {

    @Select(value = "select count(*) from t_migu_hangupsmsblackuser")
    int selectCount();

    @Insert(value = "insert into t_migu_hangupsmsblackuser (phonenumber,createtime,localid) values (#{info.phonenumber},#{info.createtime},#{info.localId})")
    void save(@Param("info") BlackUserInfo info);

    @Delete(value = "delete from t_migu_hangupsmsblackuser where phonenumber=#{phonenumber}")
    void deleteBlack(@Param("phonenumber") String phonenumber);

    @Select(value = "select phonenumber,createtime from t_migu_hangupsmsblackuser where phonenumber=#{phonenumber}")
    BlackUserInfo queryBlackUserInfo(@Param("phonenumber") String phonenumber);

    @Select(value = "<script>\n" +
                    "    select phonenumber,createtime from t_migu_hangupsmsblackuser \n" +
                    "    <where>\n" +
                    "        <if test=\"phoneList != null and phoneList.size()>0\"/>\n" +
                    "            phonenumber in\n" +
                    "        <foreach item=\"item\" index=\"index\" collection=\"phoneList\" open=\"(\" close=\")\" separator=\",\">\n" +
                    "            #{item}\n" +
                    "        </foreach>\n" +
                    "    </where>\n" +
                    "</script>")
    List<BlackUserInfo> queryBlackUserInfoList(@Param("phoneList") List<String> phoneList);

    @Insert(value = "<script>\n" +
                    " insert into t_migu_hangupsmsblackuser\n" +
                    " (phonenumber,createtime,localid)\n" +
                    " values \n" +
                    " <foreach collection=\"userList\" index=\"index\" item=\"item\" separator=\",\">\n" +
                    " (#{item.phonenumber},now(),#{item.localId})\n" +
                    " </foreach>\n" +
                    " </script>")
    void batchInsert(@Param("userList") List<BlackUserInfo> userList);

    @Delete(value = "<script>\n" +
                    "    delete from t_migu_hangupsmsblackuser \n" +
                    "    <where>\n" +
                    "        <if test=\"userList != null and userList.size()>0\"/>\n" +
                    "            phonenumber in\n" +
                    "        <foreach item=\"item\" index=\"index\" collection=\"userList\" open=\"(\" close=\")\" separator=\",\">\n" +
                    "            #{item}\n" +
                    "        </foreach>\n" +
                    "    </where>\n" +
                    "</script>")
    int batchDelete(@Param("userList") List<String> userList);

    /**
     * 增量查询
     * 
     * @param startTime
     * @param endTime
     * @param size
     * @return
     */
    @Select(value = "select phonenumber,createtime from t_migu_hangupsmsblackuser where localid=#{localId} and phonenumber>#{phoneNumber} and createtime>=#{startTime} and createtime<#{endTime} order by phonenumber limit #{size}")
    List<BlackUserInfo> selectByAdd(@Param("phoneNumber") String phoneNumber,
                                    @Param("localId") Integer localId,
                                    @Param("startTime") LocalDateTime startTime,
                                    @Param("endTime") LocalDateTime endTime,
                                    @Param("size") Integer size);

    /**
     * 全量查询
     * 
     * @param phoneNumber
     * @param size
     * @return
     */
    @Select(value = "select phonenumber,createtime from t_migu_hangupsmsblackuser where localid=#{localId} and phonenumber>#{phoneNumber} order by phonenumber limit #{size}")
    List<BlackUserInfo> selectByAll(@Param("phoneNumber") String phoneNumber,
                                    @Param("localId") Integer localId,
                                    @Param("size") Integer size);

    /**
     * 全量查询不依靠localId
     *
     * @param phoneNumber
     * @param size
     * @return
     */
    @Select(value = "select phonenumber,createtime from t_migu_hangupsmsblackuser where phonenumber>#{phoneNumber} order by phonenumber limit #{size}")
    List<BlackUserInfo> selectByAllByPhoneNumber(@Param("phoneNumber") String phoneNumber,
                                                 @Param("size") Integer size);

    /**
     * 修改数据
     * 
     * @param info
     */
    @Update(value = "update t_migu_hangupsmsblackuser set localid=#{info.localId} where phonenumber=#{info.phonenumber}")
    void update(@Param("info") BlackUserInfo info);
}
